<?php
include_once 'admin_access.php';

if(isset($_POST['submit'])){
	$user_id = intval($_POST['user_id']);
	$money = intval($_POST['money']);
	$type = intval($_POST['type']);
	
	// 被充值用户信息
	$recharged_user_info = DB::fetch_first("SELECT name, money FROM `om_user` WHERE id = $user_id");
	
	if($money <= 0){
		echo "<script>alert('提示：你输入的金额有误，请重新输入！');</script>";
	} elseif(!in_array($type, array(1, 2))){
		echo "<script>alert('提示：参数有误！');</script>";
	} else{
		recharge($user_id, $money, $type, $recharged_user_info['name'], $recharged_user_info['money']);
		echo "<script>alert('提示：充值成功！');</script>";
	}
}


function recharge($user_id, $money, $type, $recharged_user_name, $before_money){
	if($type == 2){
		$money = -$money;
	}
	DB::query("UPDATE om_user set money = money + ($money) WHERE id = " . $user_id);
	
	$ip = _get_client_ip();
	
	// 写日志表
	$data = array(
		'admin_name' => $_SESSION['user_name'], 
		'user_id' => $user_id, 
		'user_name' => $recharged_user_name, 
		'money' => $money, 
		'type' =>$type, 
		'before_money' => $before_money, 
		'after_money' => $before_money + $money, 
		'ip' => $ip, 
		'log_time' => time(), 
		);
	
	DB::insert('recharge_log', $data);
}


?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="css/indexbody.css">

</head>
<body leftmargin="8" topmargin="8" bgcolor="#FFFFFF">
<div style="min-width:780px">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="98%">
  <tbody><tr>
    <td>
     <div id="" style="float:right;padding-right:8px;">
     </div>
   </td>
  </tr>
  <tr>
    <td style="padding: 0px;" background="css/sp_bg.gif" height="1"></td>
  </tr>
</tbody></table>
<div id="__testEvn"></div>
<div id="mainmsg">
  <div class="column" id="column1"><!--左侧开始-->    
        <dl class="dbox" id="item1">
            <dt class="lside"><span class="l"> 用户充值</span></dt>
            <dd>
				<table border="1" width="99%">
					<tr>
						<th>用户ID</th>
						<th>用户名</th>
						<th>账户余额</th>
						<th>操作</th>
						<th>充值/扣费</th>
					</tr>
					<?php 
					$source = DB::query("SELECT id, name, money FROM `om_user`");
					while ($row = DB::fetch($source)){
					?>
					<form method="post">
					<input type="hidden" name="user_id" value="<?=$row['id'] ?>" />
					<tr>
						<td><?=$row['id'] ?></td>
						<td><?=$row['name'] ?></td>
						<td><?=$row['money'] ?></td>
						<td>查看充值记录</td>
						<td>
							<select name="type">
								<option value="1">充值</option>
								<option value="2">扣费</option>
							</select>
							金额：
							<input type="text" name="money" />
							<input type="submit" name="submit" value="提交" />
						</td>
					</tr>
					</form>
					<?php
					}
					?>
					
				</table>
            </dd>
        </dl>
    </div>
</div>
<br style="clear:both">
<div class="footer" align="center">
Copyright © 2004-2012
</div>
</div>
</body></html>